package game.controller
{
	import assets.EmbededAssets;
	import flash.utils.ByteArray;
	import game.constants.ApplicationNotification;
	import game.model.building.BuildingConfigProxy;
	import game.model.building.vo.BuildingConfigVO;
	import game.model.player.PlayerProxy;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * ...
	 * @author Thot
	 */
	
	public class InitGameConfigCommand extends SimpleCommand
	{
		override public function execute( notification:INotification ):void
		{
			var ba:ByteArray = (new EmbededAssets.CONFIG_FILE()) as ByteArray;
			var configFile:XML = new XML( ba.readUTFBytes( ba.length ) );
			
			_initPlayer(configFile);
			_initBuildingConfig(configFile);
			
			sendNotification( ApplicationNotification.START_GAME );
		}
		
		private function _initPlayer( p_config:XML ):void
		{
			var proxy:PlayerProxy = facade.retrieveProxy( PlayerProxy.NAME ) as PlayerProxy;
			var node:XML = p_config.player[0];
			
			
			proxy.currentPlayer.gems = (int)(node.gems);
			proxy.currentPlayer.gold = (int)(node.gold);
			proxy.currentPlayer.mana = (int)(node.mana);
			proxy.currentPlayer.level = (int)(node.level);
			proxy.currentPlayer.name = String(node.name);
			
		}
		
		private function _initBuildingConfig( p_config:XML ):void
		{
			var buildProxy:BuildingConfigProxy = facade.retrieveProxy( BuildingConfigProxy.NAME ) as BuildingConfigProxy;
			var configVO:BuildingConfigVO;
			var configNode:XML;
			
			for each( configNode in p_config.buildingsConfig.children() )
			{
				buildProxy.addBuildingConfig(BuildingConfigVO.buildFromXML(configNode));
			}
		}
	}

}